package com.tony.blog.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.tony.blog.api.utils.CommonUtil;
import com.tony.blog.model.entity.SysRoleDeptEntity;
import com.tony.blog.model.mapper.SysRoleDeptMapper;
import com.tony.blog.server.service.SysRoleDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysRoleDeptServiceImpl
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/25/10:51
 * @Description:
 */
@Service("sysRoleDeptService")
public class SysRoleDeptServiceImpl extends ServiceImpl<SysRoleDeptMapper, SysRoleDeptEntity> implements SysRoleDeptService {

    /**
    * @Description: saveOrUpdate 角色部门关系表的新增
    * @Param: [roleId, deptIdList]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 11:19
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(Long roleId, List<Long> deptIdList) {
        //清除旧数据
        deleteBatch(Arrays.asList(roleId));
        SysRoleDeptEntity entity;
        if (deptIdList != null && !deptIdList.isEmpty()) {
            for (Long dId : deptIdList) {
                entity = new SysRoleDeptEntity();
                entity.setRoleId(roleId);
                entity.setDeptId(dId);
                if (!this.save(entity)) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
    * @Description: deleteBatch 批量删除
    * @Param: [roleIds]
    * @return: boolean
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 11:31
    */
    @Override
    public boolean deleteBatch(List<Long> roleIds) {
        if (roleIds != null && !roleIds.isEmpty()) {
            String rolesIds = Joiner.on(",").join(roleIds);
            if (!baseMapper.deleteBatch(CommonUtil.concatStrToInt(rolesIds,","))) {
                return false;
            }
        }
        return true;
    }

    /**
    * @Description: queryDeptIdList 获取角色部门列表
    * @Param: [roleId]
    * @return: java.util.List<java.lang.Long>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/25 14:56
    */
    @Override
    public List<Long> queryDeptIdList(Long roleId) {
        return baseMapper.queryDeptIdListByRoleId(roleId);
    }
}
